var orderId = '';
$(function(){
	init();
	initButton();
});
//初始化页面
function init(){
	orderId = localStorage.getItem('orderId');
	console.log(orderId);
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/comment/getCommentList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			orderId:orderId,
			userId:localStorage.getItem('userid'),
			type:"all"
		},
		success : function(result) {
			console.log(result);
			var str = '<div class="comment-normal"><span>评价</span></div>';
			if (result.retCode == '0000') {
				$.each(result.data,function(i,n){
					var id = n.id||'';
					var userId = n.userid||'';
					var userName = n.username||'';
					var orderId = n.orderid||'';
					var pics = n.pics.split(',');
					var imgStr = '';
					if(pics&&pics!=''){
						imgStr = '<div class="comment-img clear">';
						$.each(pics,function(j,m){
							imgStr +='<div class="img-box fl"><img src="'+m+'" alt="" /></div>';
						});
						imgStr +='</div>';
					}
					var evaStr = '<div class="reply-box">';
					if(n.teaEvas){
						$.each(n.teaEvas,function(l,k){
							evaStr +='<div class="clear evaclear" evatype="'+k.evatype+'">';
							if(k.evatype==0){ //直接评价的
								evaStr +='<span class="fl name eva" evauserid = "'+k.evauserid+'">'+k.evausername+': </span><p class="con">'+k.msg+'</p>';
							}else{
								evaStr +='<span class="fl name eva" evauserid = "'+k.evauserid+'">'+k.evausername+'</span><span class="con fl">回复</span><span class="fl name reply" replyuserid = "'+k.replyuserid+'">'+k.replyusername+': </span><p class="con">'+k.msg+'</p>';
							}
							evaStr += '</div>';
						});
					}
					evaStr += '</div>';
					var msg = n.msg||'';
					var mark = n.mark;
					var markId = n.markid;
					var addTime = n.addtime.substring(0,10)||'';
					if(markId){
						str +='<div class="comment-list"><div class="comment-item clear"><div class="name-box fl"><p class="comment-name">'+userName+'</p><p class="comment-time">'+addTime+'</p></div><span class=" club-comment2 fr comment-eva" commentId="'+id+'" commentUserId="'+userId+'"></span><span class="zan-normal fr club-zan" markid="'+markId+'">'+mark+'</span></div><p class="comment-con">'+msg+'</p>'+imgStr+''+evaStr+'</div>';
					}else{
						str +='<div class="comment-list"><div class="comment-item clear"><div class="name-box fl"><p class="comment-name">'+userName+'</p><p class="comment-time">'+addTime+'</p></div><span class=" club-comment2 fr comment-eva" commentId="'+id+'" commentUserId="'+userId+'"></span><span class="zan-normal fr" markid="">'+mark+'</span></div><p class="comment-con">'+msg+'</p>'+imgStr+''+evaStr+'</div>';
					}
				});
			}
			console.log(str);
			$('#commentList').html(str);
		}
	});
}
//初始化绑定事件
function initButton(){
	$("body").on('click','.comment-eva',function(){
		var param = {};
		param.commentId = $(this).attr('commentId');
		param.commentUserId = $(this).attr('commentUserId');
		param.orderId = orderId;
		param.evaType = '0';
		var userId = localStorage.getItem('userid');
		if(userId == param.commentUserId){
			return;
		}
		localStorage.setItem('param',JSON.stringify(param));
		window.location = server_url + "comment2.do";
	});
	$("body").on('click','.evaclear',function(){
		var evatype = $(this).attr('evatype');
		var eva = $(this).parent().parent().find('.club-comment2');
		var commentId = eva.attr('commentId');
		var commentUserId = eva.attr('commentUserId');
		var userId = localStorage.getItem('userid');
		if(evatype==0){
			var evaUserId =$(this).find("span").attr('evauserid');
			if(evaUserId == userId){
				//自己不能回复自己的评价
				return ;
			}else{
				var param = {};
				param.commentId = commentId;
				param.commentUserId = commentUserId;
				param.orderId = orderId;
				param.replyUserId = evaUserId;
				if(userId==commentUserId){
					param.evaType = "1";
				}else{
					param.evaType = "3";
				}
				//跳转
				localStorage.setItem('param',JSON.stringify(param));
				window.location = server_url + "comment2.do";
			}
		}else{
			var userIds =$(this).find(".name");
			var evaUserId = $(userIds[0]).attr('evauserid');
			var replyUserId = $(userIds[1]).attr('replyuserid');
			
			var param = {};
			param.commentId = commentId;
			param.commentUserId = commentUserId;
			param.orderId = orderId;
			if(userId==evaUserId){
				param.replyUserId = replyUserId;
			}else{
				param.replyUserId = evaUserId;
			}
			if(userId==commentUserId){
				param.evaType = "1";
			}else if(replyUserId == commentUserId){
				param.evaType = "2";
			}else{
				param.evaType = "3";
			}
			//跳转
			localStorage.setItem('param',JSON.stringify(param));
			window.location = server_url + "comment2.do";
		}
	});
	$('body').on('click','.zan-normal', function() {
		var commentId = $(this).prev().attr('commentId');
		var favour = $(this);
		$(this).toggleClass('club-zan');
		var markId = $(this).attr('markid');
		if(!(markId=="")){
			//原先是点过赞的，现在点是为了取消点赞
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/tea/favour/deleteFavour.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					id:markId
				},
				success : function(result) {
					favour.attr('markid','');
					var num = parseInt(favour.html());
					favour.html(num-1);
				}
			});
		}else{
			//原先没有点赞现在是在点赞
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/tea/favour/addFavour.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					markId:commentId,
					userId:localStorage.getItem('userid')
				},
				success : function(result) {
					favour.attr('markid',result.data.id);
					var num = parseInt(favour.html());
					favour.html(num+1);
				}
			});
		}
	})
}